<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\AppVersion;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Redis;

class VersionController extends Controller
{
    public function versionUpdate()
    {
        //获取当前版本号和系统类型
        $version = request()->header('version');
        if (empty($version)){
            $version = Input::get('version');
        }
        $osType = request()->header('osType');
        if (empty($osType)){
            $osType = Input::get('osType');
        }

        $update = [];

        $versionInfo = AppVersion::getVersionInfo($osType);

        if (!empty($versionInfo['version'])){
            if (version_compare($version,$versionInfo['version'],'<')){
                $update['is_update'] = 1;
            }else{
                $update['is_update'] = 0;
            }
        }else{
            $update['is_update'] = 0;
        }
        $update['update_type'] = $versionInfo['update_type']??'';
        $update['update_url'] = $versionInfo['update_url']??'';
        $update['update_title'] = $versionInfo['update_title']??'';
        $update['update_content'] = $versionInfo['update_content']??'';
        $update['update_icon'] = $versionInfo['update_icon']??'';
        return jsonSuccessData(['updateInfo'=>$update]);
    }

    public function getDownloadUrl()
    {
        $android = AppVersion::getVersionInfo(1);
        $ios = AppVersion::getVersionInfo(2);

        $data = [];
        $data['download_url']['android'] = $android['update_url'];
        $data['download_url']['ios'] = $ios['update_url'];

        return jsonSuccessData($data);
    }
}
